package com.audaque.springboot.foshanupload.web.webdemo.controller;

import com.audaque.springboot.foshanupload.log.anno.WebLogAnno;
import com.audaque.springboot.foshanupload.web.anno.RAnno;
import com.audaque.springboot.foshanupload.web.webdemo.model.dto.Covert1Dto;
import com.audaque.springboot.foshanupload.web.webdemo.model.req.Covert1Req;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.PayloadApplicationEvent;
import org.springframework.context.event.SimpleApplicationEventMulticaster;
import org.springframework.web.bind.annotation.*;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @author zgb
 * @desc ...
 * @date 2023-07-30 21:49:23
 */
@Slf4j
@RestController
@RequestMapping("applicationEventMulticaster")
public class ApplicationEventMulticasterController {


    @WebLogAnno
    @GetMapping("/test")
    public void test() throws Exception {
        // 默认是同步非阻塞
        SimpleApplicationEventMulticaster multicaster = new SimpleApplicationEventMulticaster();
        // 构建线程池
        ExecutorService executor = Executors.newSingleThreadExecutor();
        // 切换成异步非阻塞
        multicaster.setTaskExecutor(executor);

        // 增加事件监听器
        multicaster.addApplicationListener(event -> { // Lambda 表达
            // 事件监听
            System.out.printf("[线程 : %s] event : %s\n",
                    Thread.currentThread().getName(), // 当前执行线程名称
                    event);
        });

        // 广播事件
        multicaster.multicastEvent(new PayloadApplicationEvent("Hello,World", "Hello,World"));

        // 关闭线程池
        executor.shutdown();


    }
}
